﻿<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" :disabled="!addState" @click="opAdd">新增</el-button>
                <el-button
                    type="info"
                    plain
                    icon="el-icon-delete"
                    :disabled="!deleteState"
                    @click="opBatchDelete"
                >删除</el-button>
                <el-button
                    type="primary"
                    icon="el-icon-plus"
                    :disabled="!addState"
                    @click="opBankInfo"
                    v-if="this.orgType==1"
                >设置银行账户</el-button>
                <el-button
                    type="primary"
                    icon="el-icon-plus"
                    :disabled="!addState"
                    @click="opInvoiceInfo"
                    v-if="this.orgType==1"
                >设置开票信息</el-button>
                <el-button
                    type="primary"
                    icon="el-icon-plus"
                    v-show="orgType==1"
                    @click="opSupplier"
                >设置还货供应商</el-button>
            </div>
            <div class="right-panel">
                <el-button :icon="Search"   type="primary" @click="opBindData">查询</el-button>
                <el-button :icon="Search" type="info" plain @click="resetSearch">重置</el-button>
                <el-button text type="primary" @click="showCondition = !showCondition">
                    <span v-if="!showCondition">展开</span>
                    <span v-else>收起</span>
                    <vab-icon
                        class="vab-dropdown"
                        :class="{ 'vab-dropdown-active': fold }"
                        icon="arrow-up-s-line"
                    />
                </el-button>
            </div>
        </el-header>
        <el-foot v-show="showCondition">
            <vab-query-form-top-panel>
                <el-form inline label-width="49px" :model="queryForm" style="margin-top: 5px;margin-right: 20px;">
                    <el-row type="flex" style="flex-wrap:wrap">
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            <el-form-item label="供应商编号" label-width="100px" style="width: 100%;">
                                <el-input
                                    placeholder="---请输入编号---"
                                    v-model="queryList.supplierCode"
                                    style="width: 100%;"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            <el-form-item label="供应商名称" label-width="100px" style="width: 100%;">
                                    <el-input
                                        placeholder="---请输入名称---"
                                        v-model="queryList.supplierName"
                                        style="width: 100%;"
                                    />
                                </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            <el-form-item label="联系人" label-width="100px" style="width: 100%;">
                                    <el-input
                                        placeholder="---请输入联系人---"
                                        v-model="queryList.realName"
                                        style="width: 100%;"
                                    />
                                </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            <el-form-item label="手机号" label-width="100px" style="width: 100%;">
                                <el-input
                                    placeholder="---请输入手机号---"
                                    v-model="queryList.mobile"
                                    style="width: 100%;"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            <el-form-item label="状态" label-width="100px" style="width: 100%;">
                                <el-select
                                    placeholder="---请选择状态---"
                                    v-model="queryList.status"
                                    style="width: 100%;"
                                >
                                    <el-option label="---请选择---" value />
                                    <el-option
                                        v-for="item in optionsState"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                   
                    
                  
                   
                    
                </el-form>
            </vab-query-form-top-panel>
        </el-foot>

        <el-main class="nopadding">
            <scTable
                ref="mytable"
                :queryType="'supplier/pages'"
                :data="listData"
                :queryList="queryList"
                :column="columnList"
                :orderList="OrderList"
                :sort-orders="['ascending', 'descending']"
                @selection-change="selectionChange"
                row-key="supplierId"
                key="supplierId"
                stripe
                show-summary
                remoteSummary
            >
                <el-table-column type="selection" width="50"></el-table-column>
                <el-table-column label="供应商编号" align="center" width="120" sortable prop="supplierCode">
                </el-table-column>
                <el-table-column label="供应商名称" align="center" width="250" sortable prop="supplierName">
                    <template #default="scope"> <span class="el-span-text" @click="opShow(scope.row, scope.$index)">{{scope.row.supplierName}}</span></template>
                </el-table-column>
                <el-table-column label="状态" fixed="right" align="center" width="120">
                    <template #default="scope">
                        <el-switch
                            v-model="scope.row.status"
                            :active-value="1"
                            :inactive-value="0"
                            @change="setStatus(scope.row)"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="是否本地供应商" fixed="right" align="center" width="150">
                    <template #default="scope">
                        <el-switch
                            v-model="scope.row.isLocal"
                            :active-value="1"
                            :inactive-value="0"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" align="center" width="180">
                    <template #default="scope">
                        <el-button-group>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                @click="opShow(scope.row, scope.$index)"
                            >查看</el-button>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                :disabled="!editState"
                                @click="opEdit(scope.row, scope.$index)"
                            >编辑</el-button>
                            <el-popconfirm
                                title="确定删除吗？"
                                @confirm="opDelete(scope.row, scope.$index)"
                            >
                                <template #reference>
                                    <el-button
                                        text
                                        type="primary"
                                        size="small"
                                        :disabled="!deleteState"
                                    >删除</el-button>
                                </template>
                            </el-popconfirm>
                        </el-button-group>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>

    <save-dialog
        v-if="dialogSave"
        ref="saveDialog"
        @fatherMethod="opBindData"
        @success="handleSaveSuccess"
        @closed="dialogSave = false"
    ></save-dialog>

    <bank-info-dialog
        v-if="dialogBankInfo"
        ref="bankInfoDialog"
        @fatherMethod="opBindData"
        @success="handleSaveSuccess"
        @closed="dialogBankInfo = false"
    ></bank-info-dialog>
    <invoice-info-dialog
        v-if="dialogInvoiceInfo"
        ref="invoiceInfoDialog"
        @fatherMethod="opBindData"
        @success="handleSaveSuccess"
        @closed="dialogInvoiceInfo = false"
    ></invoice-info-dialog>

    <supplier-dialog
        v-if="dialogSupplier"
        ref="supplierDialog"
        @fatherMethod="opBindData"
        @success="handleSaveSuccess"
        @closed="dialogSupplier = false"
    ></supplier-dialog>
</template>

<script>
import saveDialog from "./save";
import bankInfoDialog from "./bankInfo";
import invoiceInfoDialog from "./invoiceInfo";
import supplierDialog from "./backGoodsSupplier";
import publicjs from "@/api/model/publicjs";
import tool from "@/utils/tool";
import config from "@/config";
export default {
    name: "supplier",
    components: {
        saveDialog,
        bankInfoDialog,
        invoiceInfoDialog,
        publicjs,
        supplierDialog,
    },
    data() {
        return {
            dialogSave: false,
            dialogBankInfo:false,
            dialogInvoiceInfo:false,
            showCondition: false,
            dialogSupplier: false,
            selectionRows: [],
            pkList: ["supplierId"],
            columnList: [
                // {
                //     label: "供应商编号",
                //     prop: "supplierCode",
                //     width: "200",
                //     sortable: true,
                //     hide: false,
                //     fixed: false,
                    
                // },
                // {
                //     label: "供应商名称",
                //     prop: "supplierName",
                //     width: "260",
                //     sortable: true,
                //     hide: false,
                //     fixed: false,
                // },
                {
                    label: "供应商全称",
                    prop: "supplierFullName",
                    width: "260",
                    sortable: true,
                    hide: false,
                    fixed: false,
                },
                {
                    label: "联系人",
                    prop: "realName",
                    width: "180",
                    sortable: true,
                    hide: false,
                    fixed: false,
                },

                {
                    label: "手机号",
                    prop: "mobile",
                    width: "150",
                    sortable: true,
                    hide: false,
                    fixed: false,
                },
                {
                    label: "地址",
                    prop: "address",
                    width: "300",
                    sortable: true,
                    hide: false,
                    fixed: false,
                },
                {
                    label: "电子账户",
                    prop: "electronicAccount",
                    width: "200",
                    sortable: true,
                    hide: false,
                    fixed: false,
                },
                {
                    label: "余额",
                    prop: "balance",
                    width: "120",
                    sortable: true,
                    hide: false,
                    fixed: false,
                },
                // {
                //     label: "状态",
                //     prop: "statusDesc",
                //     width: "100",
                //     sortable: true,
                //     hide: false,
                //     fixed: false,
                // },
                // {
                //     label: "归属机构",
                //     prop: "orgName",
                //     width: "160",
                //     sortable: true,
                //     hide: false,
                //     fixed: false,
                // },
            ],
            OrderList: { SupplierId: "Asc" },
            queryList: {
				supplierCode:"",
				supplierName:"",
				status:""
            },
            result: "", // 运行结果
            props1: {
                checkStrictly: true,
                emitPath: false,
            },
            orgCodeList: "",
            orgNameList: "",
            addState: true, //可以增加
            editState: true, //可以修改
            editBatchState: true, //可以批量修改
            deleteState: true, //可以删除
			optionsState:[],
            orgType:3
        };
    },
    methods: {
        //添加
        opAdd() {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Add");
            });
        },
        //编辑
        opEdit(row) {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Edit").setData(row);
            });
        },
        //查看
        opShow(row) {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Show").setData(row);
            });
        },
        //设置状态
        async setStatus(row) {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    row.status == 0 ? "supplier/disabled" : "supplier/enable",
                    { supplierIds: [row.supplierId] }
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    this.$refs.mytable.getData();
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
         //设置银行账户
         opBankInfo(){
            if (this.selectionRows.length == 0) {
                this.$message.error("请选择要设置的门店");
                return;
            }

            if (this.selectionRows.length > 1) {
                this.$message.error("请选择一家门店");
                return;
            }
            this.dialogBankInfo = true;
            this.$nextTick(() => {
                this.$refs.bankInfoDialog.setData(this.selectionRows[0]);
                this.$refs.bankInfoDialog.open("Edit");
            });
        },
         //设置开票信息
         opInvoiceInfo(){
            if (this.selectionRows.length == 0) {
                this.$message.error("请选择要设置的门店");
                return;
            }

            if (this.selectionRows.length > 1) {
                this.$message.error("请选择一家门店");
                return;
            }
            this.dialogInvoiceInfo = true;
            this.$nextTick(() => {
                this.$refs.invoiceInfoDialog.setData(this.selectionRows[0]);
                this.$refs.invoiceInfoDialog.open("Edit");
            });
        },
        //删除
        async opDelete(row) {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "supplier/delete",
                    { supplierIds: [row.supplierId] }
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    this.$refs.mytable.getData();
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        //批量删除
        async opBatchDelete() {
            // eslint-disable-next-line no-debugger
            //debugger;
            this.$confirm(
                `确定删除选中的 ${this.selectionRows.length} 项吗？`,
                "提示",
                {
                    type: "warning",
                }
            )
                .then(async () => {
                    const supplierIds = this.selectionRows.map(
                        (p) => p.supplierId
                    );
                    try {
                        var returnValue =
                            await this.$API.auth.PostProcessApi.post(
                                "supplier/delete",
                                { supplierIds: [...supplierIds] }
                            );
                        if (returnValue.status == 200) {
                            this.$message.success("操作成功");
                            this.$refs.mytable.getData();
                        } else {
                            this.$message.error("操作失败:" + returnValue.msg);
                        }
                    } catch (error) {
                        this.loading = false;
                        return false;
                    }
                })
                .catch(() => {});
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selectionRows = selection;
        },
        //本地更新数据
        handleSaveSuccess(data, mode) {
            if (mode == "Add") {
                this.$refs.mytable.refresh();
            } else if (mode == "Edit") {
                this.$refs.mytable.refresh();
            }
        },
        async opBindData() {
            this.$refs.mytable.clearSort();
            this.$refs.mytable.getData();
        },
        resetSearch() {
            this.queryList={};
        },
        //设置供应商
        opSupplier() {
            if (this.selectionRows.length == 0) {
                this.$message.error("请选择要设置的供应商");
                return;
            }

            if (this.selectionRows.length > 1) {
                this.$message.error("请选择一家供应商");
                return;
            }

            console.log(this.selectionRows);
            this.dialogSupplier = true;
            this.$nextTick(() => {
                this.$refs.supplierDialog.setData(this.selectionRows[0]);
                this.$refs.supplierDialog.open("Edit");
            });
        },
        async initOptions() {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "base-dictionary/options",
                    { Codes: ["Status"] }
                );
                if (returnValue.status == 200) {
					this.optionsState=returnValue.response.find(p=>p.code=="Status").optionsDetails;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
    },
    async mounted() {
        this.orgType = tool.data.get("USER_INFO").AccountType;
        await this.initOptions();
        await this.opBindData();
    },
};
</script>
